package com.wn.config;

import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @author yellow Docter
 * @date 2022 -07-12
 * @desc
 */
public class MyBatisConfig {

    /**
     *  管理sqlSession
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

        //分页插件拦截器
        PageInterceptor pageInterceptor = new PageInterceptor();
        //配置分页插件
        factoryBean.setPlugins(
                pageInterceptor
        );
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.wn.bean");

        return factoryBean;
    }

    /**
     * 管理mapper 扫描 mapper 接口 添加到ioc容器中
     * @return
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.wn.mapper");
        return msc;
    }

}
